<template>
<!--  图形显示-->
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getCuttingMachine()">
<!--      <el-form-item>-->
<!--        <el-radio v-model="radio" label="11">全部</el-radio>-->
<!--        <el-radio v-model="radio" label="0">在线</el-radio>-->
<!--        <el-radio v-model="radio" label="1">离线</el-radio>-->
<!--        <el-radio v-model="radio" label="2">故障</el-radio>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button style="float: right;" @click="getCuttingMachine">刷新在线状态</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="24">
      <el-col :span="3" :key="index" v-for="(item,index) in this.dataList"><!-- v-show="radio == item.state || radio == '11'"-->
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span style="cursor: pointer">{{item.deviceName}}</span>
          </div>
<!--          <div class="text item">-->
<!--            <div>-->
<!--              <span>状态：</span> <span>{{item.status}}</span>-->
<!--              <div v-if="item.state==='0'" class="statestyle"></div>-->
<!--              <div v-else-if="item.state==='1'" class="statestyle_2"></div>-->
<!--              <div v-else-if="item.state==='2'" class="statestyle_3"></div>-->

<!--            </div>-->
<!--            &lt;!&ndash;          <el-button circle></el-button>&ndash;&gt;-->
<!--          </div>-->
          <div class="text item">
            <span>报警状态：</span> {{ getMessageData(item.deviceName, 'deviceStatus') }}
          </div>
          <div class="text item">
            <span>切割系统状态：</span> {{ getMessageData(item.deviceName, 'deviceCutStatus') }}
          </div>
          <div class="text item">
            <span>设备切割速度：</span> {{ getMessageData(item.deviceName, 'deviceMachineSpeed') }}
          </div>
          <div class="text item">
            <span>切割进度状态：</span> {{ getMessageData(item.deviceName, 'deviceProcessStatus') }}
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 弹窗 -->
    <cuttingpop v-if="Visiable" ref="dialog"></cuttingpop>
  </div>
</template>


<script>
  import AddOrUpdate from './cuttingmachinecurdata-add-or-update'
  import cuttingpop from './cuttingpop'
  import Cuttingpop from './cuttingpop'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        radio: '11',
        Visiable: false,
        messageData: {} // 存储消息数据
      }
    },
    components: {
      Cuttingpop,
      AddOrUpdate
    },
    activated () {
      this.getCuttingMachine()
      // this.getDataList()
    },
    mounted () {
      this.showCutData()
    },
    // computed: {
    //   filteredDataList(){
    //     return this.dataList.filter(item => item.deviceName.endsWith('切割机'));
    //   }
    // },
    methods: {
      //测试ws数据
      showCutData(){
        let URL = "ws://10.143.7.73:8089/renren-fast/websocket/font";
        // let URL = "ws://localhost:8089/renren-fast/websocket/font";
        // let URL = "ws://101.43.150.26:8089/renren-fast/websocket/font";
        let ws = new WebSocket(URL);
        ws.onmessage = evt => {    //后端发来的消息接收（注意数据放在evt的data中并且是个JSON字符串，需要parse成JSON对象去使用）
          //let hhlist = JSON.parse(evt);

          console.log("接收中。。。")
          console.log(evt.data)
          //evt.data格式为：{"板材切割中心3#切割机.deviceProcessStatus":"6","板材切割中心3#切割机.deviceCutStatus":"1","板材切割中心3#切割机.deviceMachineSpeed":"0","板材切割中心3#切割机.deviceLocation":"0","板材切割中心3#切割机.deviceStatus":"0"}
          console.log('接收完成')
          //具体业务逻辑
          const messageData = JSON.parse(evt.data);
          console.log(messageData)
          this.updateMessageData(messageData);

        }
        ws.onclose = function () {
          // 关闭 websocket
          console.log('连接已关闭...')
        }
        // 路由跳转时结束websocket链接
        this.$router.afterEach(function () {
          ws.close()
        })
      },
      getMessageData(deviceName, key) {
        if (deviceName in this.messageData && key in this.messageData[deviceName]) {
          return this.messageData[deviceName][key];
        }
        return '0';
      },
      updateMessageData(messageData) {
        for (const key in messageData) {
          const deviceName = key.split('.')[0];
          const deviceProperty = key.split('.')[1];
          if (!(deviceName in this.messageData)) {
            this.$set(this.messageData, deviceName, {});
          }
          this.$set(this.messageData[deviceName], deviceProperty, messageData[key]);
        }
      },
      //所有切割机
      getCuttingMachine(){
        this.dataListLoading = true
        this.$http({
          // url: this.$http.adornUrl('/cuttingcur/cuttingmachinecurdata/getCuttingMachine'),
          url: this.$http.adornUrl('/cuttingcur/cuttingmachinecurdata/listCuttinginfo'),
          method: 'get'
          // ,
          // params: this.$http.adornParams({
          //   'cncName': this.dataForm.key
          // })
        }).then(({data}) => {
          if (data) {
            console.log(data)
            this.dataList = data.filter(item => item.deviceName.endsWith('切割机'));
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      get_val (index) {
        // console.log(this.stateList[index])
        this.Visiable = true;
        this.$nextTick(() => {
          this.$refs.dialog.init(index);
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/cuttingcur/cuttingmachinecurdata/listAll'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          // console.log(data)
          // if (data && data.code === 0) {
            this.dataList = data
            console.log(this.dataList)
          // } else {
          //   this.dataList = []
          //   this.totalPage = 0
          // }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.no
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/cuttingcur/cuttingmachinecurdata/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style lang="scss">
.tr-color-0 {
  background: #f0f0f0;
}
.tr-color-1 {
  background: #fff;
}
.el-table .success-row {
  background: #DFEAF5;
}
.statestyle{
  width:20px;
  height:20px;
  border: solid 1px black;
  display: inline;
  float: right;
  border-radius: 50%;
  background-color: red
}
.statestyle_2{
  width:20px;
  height:20px;
  border: solid 1px black;
  display: inline;
  float: right;
  border-radius: 50%;
  background-color: black;
}
.statestyle_3{
  width:20px;
  height:20px;
  border: solid 1px black;
  display: inline;
  float: right;
  border-radius: 50%;
  background-color: skyblue;
}
.text {
  font-size: 10px;
}

.item {
  margin-bottom: 18px;
  width: 200px;
  height: 20px
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 150px;
  //height: 200px;
}
.mod-home {
  line-height: 1.5;
}
.mod-demo-echarts {
  > .el-alert {
    margin-bottom: 10px;
  }
  > .el-row {
    margin-top: -10px;
    margin-bottom: -10px;
    .el-col {
      padding-top: 10px;
      padding-bottom: 10px;
    }
  }
  .chart-box {
    min-height: 400px;
  }
  .dotClass {
    width:10px;
    height:10px;
    border-radius: 50%;
    display: block;
    margin-left: 10px;  //这个用于圆点居中
  }
}
</style>
